#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    int e = 0, i1, j1, a[100][100] = { 0 }, row, col, sum = 0;
    scanf("%d %d", &row, &col);
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
            scanf("%d", &a[i][j]);
    i1 = row - 1;
    j1 = col;
    int i = 0, j = 0, temp;
    while (sum != row * col)
    {
        if (e % 4 == 0)
        {
            for (temp = 0; temp < j1; temp++)
            {
                printf("%d\n", a[i][j + temp]);
                sum++;
            }
            j += temp - 1;
            i++;
            j1--;

        }
        else if (e % 4 == 1)
        {
            for (temp = 0; temp < i1; temp++)
            {
                printf("%d\n", a[i + temp][j]);
                sum++;
            }
            i += temp - 1;
            j--;
            i1--;

        }
        else if (e % 4 == 2)
        {
            for (temp = 0; temp < j1; temp++)
            {
                printf("%d\n", a[i][j - temp]);
                sum++;
            }
            j = j - (temp - 1);
            j1--;
            i--;

        }
        else if (e % 4 == 3)
        {
            for (temp = 0; temp < i1; temp++)
            {
                printf("%d\n", a[i - temp][j]);
                sum++;
            }
            i -= temp - 1;
            i1--;
            j++;

        }
        e++;
    }
    return 0;
}